package com.eurasia.assignment.controller;

import com.eurasia.assignment.domain.Achievement;
import com.eurasia.assignment.service.AchievementService;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/achievements")
public class AchievementController {
    private final AchievementService service;

    public AchievementController(AchievementService service) {
        this.service = service;
    }

    @PostMapping
    public ResponseEntity<Map<String, Object>> createAchievement(
            @RequestParam("name") String name,
            @RequestParam("achievement_type") String achievementType,
            @RequestParam("subtype") String subtype,
            @RequestParam("specific_type") String specificType,
            @RequestParam(value = "description", required = false) String description,
            @RequestParam(value = "file", required = false) MultipartFile file) {
        try {
            Achievement achievement = service.createAchievement(name, achievementType, subtype, specificType, description, file);
            Map<String, Object> response = new HashMap<>();
            response.put("status", "success");
            response.put("data", achievement);
            return new ResponseEntity<>(response, HttpStatus.CREATED);
        } catch (IllegalArgumentException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("status", "error");
            response.put("message", e.getMessage());
            return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
        } catch (IOException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("status", "error");
            response.put("message", "File upload failed");
            return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @GetMapping
    public ResponseEntity<Map<String, Object>> getAllAchievements() {
        List<Achievement> achievements = service.getAllAchievements();
        Map<String, Object> response = new HashMap<>();
        response.put("status", "success");
        response.put("data", achievements);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getAchievementById(@PathVariable Long id) {
        try {
            Achievement achievement = service.getAchievementById(id);
            Map<String, Object> response = new HashMap<>();
            response.put("status", "success");
            response.put("data", achievement);
            return new ResponseEntity<>(response, HttpStatus.OK);
        } catch (EntityNotFoundException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("status", "error");
            response.put("message", e.getMessage());
            return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
        }
    }

    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> updateAchievement(
            @PathVariable Long id,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "achievement_type", required = false) String achievementType,
            @RequestParam(value = "subtype", required = false) String subtype,
            @RequestParam(value = "specific_type", required = false) String specificType,
            @RequestParam(value = "description", required = false) String description,
            @RequestParam(value = "file", required = false) MultipartFile file) {
        try {
            Achievement achievement = service.updateAchievement(id, name, achievementType, subtype, specificType, description, file);
            Map<String, Object> response = new HashMap<>();
            response.put("status", "success");
            response.put("data", achievement);
            return new ResponseEntity<>(response, HttpStatus.OK);
        } catch (EntityNotFoundException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("status", "error");
            response.put("message", e.getMessage());
            return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
        } catch (IOException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("status", "error");
            response.put("message", "File upload failed");
            return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteAchievement(@PathVariable Long id) {
        try {
            service.deleteAchievement(id);
            Map<String, Object> response = new HashMap<>();
            response.put("status", "success");
            response.put("message", "Achievement deleted");
            return new ResponseEntity<>(response, HttpStatus.OK);
        } catch (EntityNotFoundException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("status", "error");
            response.put("message", e.getMessage());
            return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
        }
    }
}

